import javax.microedition.media.*;
import javax.microedition.media.control.*;
public class Sound
{
 public  static int s_iCurSoundID = -1;
 public  static boolean s_sound_enable ;
  
  private static Player [] s_players = new Player[DEF.MAX_SOUND_COUNTS];
  
  private GameEngine m_ge;
  Sound(GameEngine ge)
  {
	  m_ge= ge;
  }
  void loadSound()
  {
    
    try
    {
      for (int id = 0; id < DEF.MAX_SOUND_COUNTS; id++)
      {
    	  java.io.InputStream is = m_ge.getClass().getResourceAsStream("/"+DEF.SOUND_ITEMS_LIST[id]);///(DEF.SOUND_ITEMS_LIST[id]);
    	  s_players[id] = Manager.createPlayer(is, "audio/midi"); //id >= DEF.SOUND_NOTALLOWED ? "audio/x-wav" : "audio/midi");

      //  is.close();b
       // is = null;
      }
      //in_file.close();
    }
    catch (Exception ex)
    {
      {
        ex.printStackTrace();
        //errormsg = ex.getMessage();
      }
    }
    //in_file = null;
    System.gc();
  }

  public void setSound(boolean bs)
  {
	  s_sound_enable = bs;
  }
  
  public void setSound()
  {
	  s_sound_enable = !s_sound_enable;
  }
  
  public boolean getSoundStatus()
  {
	 return  s_sound_enable ;
  }
  
  public void soundPlay(int id)
  {
    if (!s_sound_enable)
      return;
    
    try
    {
      soundStop();
      if (s_players[id] != null)
      {
    	 // s_players[id].addPlayerListener(this);
    	  s_players[id].realize();
    	  s_players[id].prefetch();
    	  s_players[id].start();

        s_iCurSoundID = id;
      }
    }
    catch (Exception e)
    {
      e.printStackTrace();
      s_iCurSoundID = -1;
    }
  }

  public void soundStop()
  {
    int id = s_iCurSoundID;
    if (id < 0 || s_players[id] == null )
    {
      s_iCurSoundID = -1;
      return;
    }

    try
    {
    	///s_players[id].removePlayerListener(this);
      try
      {
    		///s_players[id].stop();
        if (s_players[id].getState() == Player.STARTED)
        {
        	s_players[id].stop();
      //    if (s_players[id].getState() != Player.PREFETCHED)
//            Thread.yield();
        }
//
        if (s_players[id].getState() == Player.PREFETCHED)
        {
        	s_players[id].deallocate();
//          if (s_players[id].getState() != Player.REALIZED)
//            Thread.yield();
        }
      }
      catch (MediaException ex)
      {
        {
          ex.printStackTrace();
        }

      }
      s_iCurSoundID = -1;
    }
    catch (Exception e)
    {
      {
        e.printStackTrace();
      }
    }
  }

  public void playerUpdate(Player player, String event, Object eventData)
  {
    try
    {
      if (event .equals(PlayerListener.END_OF_MEDIA) || event .equals( PlayerListener.ERROR))
      {
        soundStop();
      }
      else if (event .equals( PlayerListener.STOPPED))
      {
        soundStop();
      }
      
    }
    catch (Exception ex)
    {
      {
        ex.printStackTrace();
      }
      s_iCurSoundID = -1;
    }
  }
}